package server_center

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/server_center"
	server_centerReq "github.com/flipped-aurora/gin-vue-admin/server/model/server_center/request"
)

type SysLogService struct {
}

// CreateSysLog 创建sysLog表记录
// Author [piexlmax](https://github.com/piexlmax)
func (sysLogService *SysLogService) CreateSysLog(sysLog *server_center.SysLog) (err error) {
	err = global.GVA_DB.Create(sysLog).Error
	return err
}

// DeleteSysLog 删除sysLog表记录
// Author [piexlmax](https://github.com/piexlmax)
func (sysLogService *SysLogService) DeleteSysLog(sysLog server_center.SysLog) (err error) {
	err = global.GVA_DB.Delete(&sysLog).Error
	return err
}

// DeleteSysLogByIds 批量删除sysLog表记录
// Author [piexlmax](https://github.com/piexlmax)
func (sysLogService *SysLogService) DeleteSysLogByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]server_center.SysLog{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateSysLog 更新sysLog表记录
// Author [piexlmax](https://github.com/piexlmax)
func (sysLogService *SysLogService) UpdateSysLog(sysLog server_center.SysLog) (err error) {
	err = global.GVA_DB.Save(&sysLog).Error
	return err
}

// GetSysLog 根据id获取sysLog表记录
// Author [piexlmax](https://github.com/piexlmax)
func (sysLogService *SysLogService) GetSysLog(id uint) (sysLog server_center.SysLog, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&sysLog).Error
	return
}

func (sysLogService *SysLogService) GetSysLogById(id1 uint, id2 string, id3 string) (sysLog server_center.SysLog, err error) {
	err = global.GVA_DB.Where("project_id = ? and sys_user_name= ? and  DATE_FORMAT(created_at, '%Y-%m-%d') = ?  ", id1, id2, id3).Find(&sysLog).Error
	return
}

// GetSysLogInfoList 分页获取sysLog表记录
// Author [piexlmax](https://github.com/piexlmax)
func (sysLogService *SysLogService) GetSysLogInfoList(info server_centerReq.SysLogSearch, names []string) (list []server_center.SysLog, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&server_center.SysLog{})
	var sysLogs []server_center.SysLog
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.SuperiorsName != "" {
		db = db.Where("superiors_name = ?", info.SuperiorsName)
	}
	if info.SysUserName != "" {
		db = db.Where("sys_user_name = ?", info.SysUserName)
	}
	if info.ProjectName != "" {
		db = db.Where("project_name = ?", info.ProjectName)
	}
	if info.ProjectId > 0 {
		db = db.Where("project_id = ?", info.ProjectId)
	}
	if len(names) > 0 {
		db = db.Where("sys_user_name in ?", names)
	}

	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&sysLogs).Error
	return sysLogs, total, err
}
